using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sec_scan漏洞扫描系统
{
    public partial class Sec_Scan_FingeAdd : Form
    {
        public Sec_Scan_FingeAdd()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Close();
        }
        public int oldX, oldY;
        public string code;//验证码

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            //判断是否为鼠标左键
            if (e.Button == MouseButtons.Left)
            {
                //获取鼠标左键按下时的位置
                this.oldX = e.Location.X;
                this.oldY = e.Location.Y;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(watermarkTextBox1.Text)|| string.IsNullOrEmpty(watermarkTextBox2.Text))
            {
                MessageBox.Show("数据不能为空！");
            }
            else
            {
                //执行指纹添加
                FingerAdd();
            }
        }
        /// <summary>
        /// 指纹添加
        /// </summary>
        public void FingerAdd()
        {
            //指纹数据加密
            string fingepath = Sec_Config.Sec_Base64Code.Tobase64(watermarkTextBox2.Text);//指纹有效载荷
            string fingekey = Sec_Config.Sec_Base64Code.Tobase64(watermarkTextBox3.Text);//指纹关键特征
            //string fingewafkey = Sec_Config.Sec_Base64Code.Tobase64(watermarkTextBox9.Text);//指纹WAF关键特征

            string key_1 = Sec_Api.Sec_DesThreDesApi.DesKeyInfo(Sec_Api.Sec_DesThreDesApi.D1);//密文一
            string key_2 = Sec_Api.Sec_DesThreDesApi.DeskeyInfoOne(Sec_Api.Sec_DesThreDesApi.D1);//密文二
            string key_3 = Sec_Api.Sec_DesThreDesApi.DeskeyInfoTwo(Sec_Api.Sec_DesThreDesApi.D1);//密文三

            string path = Sec_Config.Sec_DesThreDes.Encrypt(fingepath, key_1, key_2, key_3);//对有效载荷进行加密
            string key = Sec_Config.Sec_DesThreDes.Encrypt(fingekey, key_1, key_2, key_3);//对指纹关键特征进行加密
            //string waf = Sec_Config.Sec_DesThreDes.Encrypt(fingewafkey, key_1, key_2, key_3);//对waf关键特征进行加密

            string sql = Sec_Api.Sec_AllAPI.Sqlclient(Sec_Api.Sec_LoopScan_Service_Api.P1);
            SQLiteConnection connection = new SQLiteConnection(sql);
            try
            {
                connection.Open();
                if(connection.State == ConnectionState.Open)
                {
                    string sqls = "INSERT INTO Sec_FingerPrint (fingepath, fingekey, fingename, fingewafname, fingewafkey, fingewafform, fingeip, systemname, systemversion, systemtype, testaddress, addtime, code) VALUES('"+path+ "', '" + key + "', '" + watermarkTextBox1.Text + "', '" + watermarkTextBox8.Text + "', '" + watermarkTextBox9.Text + "', '" + watermarkTextBox10.Text + "', '" + watermarkTextBox7.Text + "', '" + watermarkTextBox4.Text + "', '" + comboBox1.Text + "', '" + comboBox2.Text + "', '" + watermarkTextBox7.Text + "', '"+DateTime.Now+"', '"+watermarkTextBox11.Text+"')";
                    SQLiteCommand command = new SQLiteCommand(sqls, connection);
                    command.ExecuteReader();
                    MessageBox.Show("数据添加成功！");
                    Close();//关闭窗体
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            //刷新验证码
            ReferCode();
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //计算鼠标移动距离
                this.Left += e.Location.X - this.oldX;
                this.Top += e.Location.Y - this.oldY;
            }
        }

        private void Sec_FingeAdd_Load(object sender, EventArgs e)
        {
            ReferCode();//获取验证码
        }

        public void ReferCode()
        {
            code = Sec_Config.Sec_CodeInfo.GenerateCheckCode();
            Bitmap image = Sec_Config.Sec_CodeInfo.CreateCheckCodeImage(code, 64, 30);//长高
            pictureBox2.Image = image;
        }
    }
}
